#include "PositionControllerComponent.h"
#include "GameObjectSystem.h"
#include "KeyboardInputManager.h"
#include "PositionControllerProperty.h"
#include "position.h"
#include "ComboControllerProperty.h"
#include "physicsBody.h"
#include "physicsComponent.h"

const StringHash PositionControllerComponent::POSITION_CONTROLLER_ID= StringHash("posController");

PositionControllerComponent::PositionControllerComponent()
	:Component(POSITION_CONTROLLER_ID, 0)
{
}

PositionControllerComponent::~PositionControllerComponent()
{
}

void PositionControllerComponent::update(real frametime, real timestep)
{
	GameObjectSystem& gameObjectSystem= GameObjectSystem::GetSingleton();
	if(gameObjectSystem.isInPause())
	{
		return;
	}
	KeyboardInputManager& keyboardInputManager= KeyboardInputManager::GetSingleton();
	const ObjectPropertyTable* positionPropertyTable= gameObjectSystem.getProperties(PositionControllerProperty::POSITION_CONTROLLER_PROPERTY_ID);
	if(positionPropertyTable != NULL)
	{
		for(ObjectPropertyTable::const_iterator opIt= positionPropertyTable->begin(); opIt != positionPropertyTable->end(); ++opIt)
		{
			const PositionControllerProperty* positionController= static_cast<const PositionControllerProperty*>(opIt->second);
			ObjectProperty* positionProperty= gameObjectSystem.editProperty(Position::POSITION_ID, positionController->getObjectId());
			if(positionProperty != NULL)
			{
				// Get the ComboContollerComponent linked with this object.
				ObjectProperty* comboProperty = gameObjectSystem.editProperty(ComboControllerProperty::COMBO_CONTROLLER_PROPERTY_ID, positionController->getObjectId());
				if (comboProperty != NULL)
				{
					ComboControllerProperty* combo = static_cast<ComboControllerProperty*>(comboProperty);
			
					// If the combo is in progress (or completed), update its timer.
					if (combo->inProgress())
						combo->updateTimer(frametime);
					
					if (keyboardInputManager.getKeyState(combo->getKey1()))
					{
						combo->startCombo(); // Set combo as in progress.
					}
					else if (combo->inProgress() && keyboardInputManager.getKeyState(combo->getKey2()))
					{
						combo->completeCombo(); // Set combo as completed.
					}
				}

				Position* position= static_cast<Position*>(positionProperty);
				matrix matPos= position->getPosition();
				if(keyboardInputManager.getKeyState(positionController->getUp()) == 0 || keyboardInputManager.getKeyState(positionController->getDown()) == 0)
				{
					vector3 translation;
					GetTranslation(matPos, &translation);
					if( keyboardInputManager.getKeyState(positionController->getUp()) > 0 && translation.y > -82)
					{
						// Cancel combo.
						if (comboProperty != NULL)
						{
							ComboControllerProperty* combo = static_cast<ComboControllerProperty*>(comboProperty);
							combo->stopCombo();
						}

						AddTranslation(&matPos, vector3(0.f, -1.f, 0.f));
						position->setPosition(matPos);
					}
					else if(keyboardInputManager.getKeyState(positionController->getDown()) > 0 && translation.y < 82)
					{
						// Cancel combo.
						if (comboProperty != NULL)
						{
							ComboControllerProperty* combo = static_cast<ComboControllerProperty*>(comboProperty);
							combo->stopCombo();
						}

						AddTranslation(&matPos, vector3(0.f, 1.f, 0.f));
						position->setPosition(matPos);
					}
				}
			}
		}
	}
}